import 'dart:convert';
import 'dart:io';

import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:zego_express_screen_capture/xx/srt_animtaion.dart';
import 'package:zego_express_screen_capture/xx/srt_transform.dart';

class ZimuPage extends StatefulWidget {
  const ZimuPage({Key? key}) : super(key: key);

  @override
  _ZimuPageState createState() => _ZimuPageState();
}

class _ZimuPageState extends State<ZimuPage> {
  @override
  void initState() {
    super.initState();
    init();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Column(
        children: [
          Text(xList.toString(), style: TextStyle(fontSize: 14)),
          MaterialButton(
              child: Text("选择"),
              onPressed: () {
                pick();
              }),
          MaterialButton(
              child: Text("进入"),
              onPressed: () {
                if (strList.isEmpty) {
                  return;
                }

                Future future = Navigator.of(context)
                    .push(MaterialPageRoute(builder: (context) {
                  return new AAA(
                    list: strList,
                    title: name,
                    index: index,
                    subTitle: subTitle,
                  );
                }));
              }),
          // Text(_content ?? "",
          //     style: TextStyle(
          //       fontSize: 14,
          //     )),

          Expanded(
            child: ListView.builder(
              itemBuilder: (context, index) {
                return Text(
                  strList[index].toString(),
                  style: TextStyle(fontFamily: "Pmzdzt"),
                );
              },
              itemCount: strList.length,
            ),
          ),
        ],
      ),
    );
  }

  String? _content;
  String? subTitle;
  String? index;
  String name = "";

  List<String> list = [];

  List<SrtModel> strList = [];

  pick() async {
    FilePickerResult? result = await FilePicker.platform.pickFiles();

    if (result != null) {
      // ignore: unused_local_variable
      File file = File(result.files.single.path!);

      String fileName = result.files.single.name;

      List<String> split = fileName.split('.');

      name = split.first;
      if (split.length > 2) {
        subTitle = split[2];
        index = split[1];
      }

      _content = await file.readAsString();
      list = await file.readAsLines();

      strList = SrtTransform.getList(list);

      setState(() {});
    } else {
      // User canceled the picker
    }
  }

  init() {
    String str = "00:00:02,133 --> 00:00:03,333";

    List<String> list = str.split("-->");

    DateTime start = DateTime.parse("1970-01-01 " + list.first.trim())
        .add(Duration(hours: 8));
    DateTime end = DateTime.parse("1970-01-01 " + list.last.trim())
        .add(Duration(hours: 8));

    print("Start:" + start.millisecondsSinceEpoch.toString());
    print("end:" + end.millisecondsSinceEpoch.toString());
  }

  List<int> xList = [];

  // ignore: unnecessary_statements, unnecessary_statements

}
